package com.ruoyi.deeplearn.component;

import com.ruoyi.deeplearn.domain.DeepMarkTask;
import com.ruoyi.deeplearn.service.IDeepMarkTaskService;
import com.ruoyi.deeplearn.task.PublishTaskLayerTask;
import com.ruoyi.deeplearn.vo.PublishTaskLayerVo;
import com.ruoyi.gis.geoserver.service.GeoserverService;
import com.ruoyi.gis.service.IGisLayerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class PublishTaskLayerProcess {
    private final Logger logger = LoggerFactory.getLogger(PublishTaskLayerProcess.class);
    @Autowired
    @Qualifier("publishTaskLayerTaskExecutor")
    private ThreadPoolTaskExecutor taskExecutor;
    @Autowired
    private IGisLayerService gisLayerService;
    @Autowired
    private GeoserverService geoserverService;
    @Autowired
    private IDeepMarkTaskService deepMarkTaskService;

    public void sendPublishLayerMessage(PublishTaskLayerVo publishTaskLayerVo) {
        if(publishTaskLayerVo==null||publishTaskLayerVo.getTaskId()==null)return;
        taskExecutor.submit(new PublishTaskLayerTask(publishTaskLayerVo));
    }

//    public boolean cancelFilePublish(Long id) {
//        DeepMarkTask markTask = deepMarkTaskService.selectDeepMarkTaskById(id);
//        if(markTask==null||tblFile.getPublished()==null||!tblFile.getPublished()||tblFile.getLayerId()==null){
//            logger.info("图层未发布........");
//            return false;
//        }
//        GisLayer gisLayer = gisLayerService.selectGisLayerByLayerId(tblFile.getLayerId());
//        if(gisLayer==null)return false;
//        logger.info("geoserver remove layer {} {} ",gisLayer.getWorkspace(),gisLayer.getLayerName());
//        try {
//            geoserverService.removeLayer(gisLayer.getStoreType(),
//                    gisLayer.getWorkspace(),gisLayer.getStore(),
//                    gisLayer.getLayerName());
//        } catch (IOException e) {
//            e.printStackTrace();
//            logger.info("delete layer error ................................");
//        }
//        logger.info("delete  layer info ");
//        gisLayerService.deleteGisLayerByLayerId(gisLayer.getLayerId());
//        logger.info("update file publish state ");
//        //检查他的父下是否还有图层，如果没有，就删除该组
//        gisLayerService.cleanLayerGroup(gisLayer.getParentId());
//        tblFile.setPublished(false);
//        tblFile.setLayerId(null);
//        tblFileService.updateTblFile(tblFile);
//        return true;
//    }
//
//    public void publishLayer(List<FilePublishDto> publishFilesMsg) {
//        logger.info("the file is Publishable...........will publish layer count {}...............",publishFilesMsg.size());
//        publishFilesMsg.stream().forEach(publishDto->{
//            sendPublishLayerMessage(publishDto);
//        });
//        logger.info("saveFile...........end...............");
//    }
}
